package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 通知设置服务接口实现类
 */
@DubboService
public class SettingsApiImpl implements SettingsApi{
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 根据当前用户id查询tb_settings表得到设置内容
     *
     * @param userId
     * @return
     */
    @Override
    public Settings findSettingsById(Long userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 通知设置 - 保存
     *
     * @param settings
     */
    @Override
    public void add(Settings settings) {
        //1.查询tb_settings表记录是否存在
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", settings.getUserId());
        Settings st = settingsMapper.selectOne(queryWrapper);
        //2.不存在 保存
        if (st == null) {
            settingsMapper.insert(settings);
        }else {
            //3.存在 更新
            settings.setId(st.getId());//将数据库查询的主键id 设置到settings对象中
            settingsMapper.updateById(settings);
        }
    }
}
